// pages/myRegister/myRegister.js
import { areaList } from '../../dist/area-data/dist/index.cjs';
import Toast from '../../dist/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList:areaList,
    templateData:[{idx:0,name:"真实姓名",preTmp:"请输入姓名",pre:"请输入姓名",type:"text",txt:"",disabled:false,inputStyle:""},
                  {idx:1,name:"身份证号",preTmp:"请输入身份证号",pre:"请输入身份证号",type:"idcard",txt:"",disabled:false,inputStyle:""},
                  {idx:2,name:"所在地",preTmp:"请输入所在地",pre:"请输入所在地",type:"text",txt:"",disabled:true,inputStyle:""},
                  {idx:3,name:"手机号",preTmp:"请输入手机号",pre:"请输入手机号",type:"number",txt:"",disabled:false,inputStyle:""},
                  {idx:4,name:"验证码",preTmp:"请输入验证码",pre:"请输入验证码",type:"number",txt:"",disabled:false,btn:true,inputStyle:"",btnStr:"发送"}
    ],
    popArea:false,
    objAddress:{},
    alertIsHidde:true,
    radioSelect:false,
    radioColor:"rgb(255, 255, 255)",
    timeId:0,
    cd:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '完善信息',
    })
  },
  onSaveClick(e){
    const datas = this.data.templateData;
    var data;
    for(var i=0; i<datas.length; i++){
      if(!datas[i].txt){
        data = datas[i];
        break;
      }
    }
    if(!this.data.radioSelect){
      Toast("请阅读并接受 注册须知");
      return;
    }
    //这里后面要加上 短线验证码的验证...
    //.....

    if(data){
      Toast("请输入"+data.name);
    }
    else{
      //保存操作。。。
      const app = getApp();
      const curdata = {
        type:"setUserRegister",
        openid:app.globalData.openid,
        regInfo:{
          name:datas[0],
          icard:datas[1],
          address:datas[2],
          phone:datas[3]
        }
      }
      const _this = this;
      wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data:curdata,
        complete: res => {
          console.log('setOrderHotels: ', res);
          _this.onCallBack();
        }
      })
    }
  },
  onCallBack(){
    wx.navigateBack({
      delta: 1
    })
  },
  onSendCode(e){
    console.log(e);
    const data = this.data.templateData[3];
    if(!data.txt){
      Toast("手机号不能为空");
    }
    else{
      //随机生成6位数 验证码
      const code = Nath.floor(Math.random() * 899999) + 100000;
      wx.cloud.callFunction({
        name: 'quickstartFunctions',
        data: {
          type: 'sendSms',
          content: "【龙游文旅】您的验证码:" + code + "将在1分钟后失效，请尽快使用，谨防诈骗！",
          phone: "+8613923734886"
        },
      })
      .then((resp) => {
        console.log("-->短信返回");
        console.log(resp);
        if(resp.result){
          Toast(resp.result.errCode+"\n"+resp.result.errMsg);
        }

      })
      .catch((e) => {
        console.log("-->短信返回2");
        console.log(e);
      });
    }
  },
  onInput(e){
    const idx = e.currentTarget.dataset.index;
    var value = e.detail.value;
    var datas = this.data.templateData;
    if(value){
      console.log("idx="+idx+",value="+value);
      datas[idx].txt = value;
      datas[idx].pre = "";
      this.setData({templateData:datas});
    }
    else{
      datas[idx].pre = datas[idx].preTmp;
      this.setData({templateData:datas});
    }
  },
  onRadioClick(e){
    var select = !this.data.radioSelect;
    var color;
    if(select){
      color = "rgb(255, 215, 0)";
    }
    else{
      color = "rgb(255, 255, 255)";
    }
    this.setData({radioSelect:select,radioColor:color});
  },
  onPopAlert(e){
    this.setData({alertIsHidde:false});
  },
  onAlertEvent(e){
    if(e.detail.type == "confirm"){
      var select = true;
      var color = "rgb(255, 215, 0)";
      this.setData({radioSelect:select,radioColor:color});
    }
    this.setData({alertIsHidde:true});
  },
  onAreaConfirm(e){
    console.log(e);
    var obj = this.data.templateData[2];
    if(e.type == "confirm"){
      const arr = e.detail.values;
      var str = "";
      for(var i=0; i<arr.length; i++){
        str += arr[i].name;
        if(i < arr.length - 1){
          str += "，";
        }
      }
      obj.pre = str;
      obj.txt = str;
      obj.inputStyle = "color:black;";
    }
    else{
      obj.pre = "请输入所在地";
      obj.txt = "";
      obj.inputStyle = "";
    }
    this.setData({popArea:false,objAddress:obj,templateData:this.data.templateData});
  },
  onMaskClick(e){
    this.setData({popArea:false});
  },
  onAddressClick(e){
    //console.log(e);
    const isPop = e.currentTarget.dataset.ispop;
    if(isPop){
      this.setData({popArea:!this.data.popArea});
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})